﻿using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;
using CKS.Core.Framework;
using System.Diagnostics;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 子篮记录Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksBasketrecordService), ServiceLifetime = LifeTime.Transient)]
    public class CksBasketrecordService : BaseService<CksBasketDyneRecord>, ICksBasketrecordService
    {
        /// <summary>
        /// 查询子篮记录列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksBasketDyneRecordDto> GetList(CksBasketDyneRecordQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .OrderByDescending(a=>a.Id)
                .ToPage<CksBasketDyneRecord, CksBasketDyneRecordDto>(parm);
            string sql = Queryable()
                .Where(predicate.ToExpression()).ToSqlString();
            Debug.WriteLine(sql);
            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public CksBasketDyneRecord GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加子篮记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CksBasketDyneRecord AddCksBasketrecord(CksBasketDyneRecord model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改子篮记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCksBasketrecord(CksBasketDyneRecord model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<CksBasketDyneRecord> QueryExp(CksBasketDyneRecordQueryDto parm)
        {
            var exp = Expressionable.Create<CksBasketDyneRecord>();
            exp.AndIF(parm.BeginTime != DateTime.MinValue && parm.BeginTime != null, u => u.CreatedTime >= parm.BeginTime.ToBeginTime());
            exp.AndIF(parm.EndTime != DateTime.MinValue && parm.EndTime != null, u => u.CreatedTime <= parm.EndTime.ToEndTime());
            exp.AndIF(!parm.BlueCode.IsEmpty(),u=>u.BlueCode == parm.BlueCode );
            exp.AndIF(parm.Type!=null&&parm.Type.Value>-1, u => u.Type == parm.Type.Value);
            return exp;
        }
    }
}